

#import "NVWebViewController.h"
#import "MsgManager.h"

@implementation NVWebViewController

@synthesize webView;
@synthesize errorLabel;
@synthesize retryButton;
@synthesize disableWebTitle;

- (void)viewDidLoad {
	[super viewDidLoad];
	self.navigationItem.title = @"米湖";
	if(titleLoadingIndicator) {
		UIBarButtonItem *loadingBar = [[UIBarButtonItem alloc] initWithCustomView:titleLoadingIndicator];
		self.navigationItem.rightBarButtonItem = loadingBar;
	}
//	[retryButton setAdditionStyle:NVButtonBackgroundNormal];
}

#pragma mark -
#pragma mark UIWebViewDelegate Methods

- (BOOL)webView:(UIWebView *)web shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
	NSURL *url = [request URL];
	NSString *sUrl = [url absoluteString];
	if(!([@"http" isEqual:[url scheme]] || [@"https" isEqual:[url scheme]])) {
		[[UIApplication sharedApplication] openURL:url];
		return NO;
	}
	if([@"http" isEqual:[url scheme]]
	   && ([sUrl hasPrefix:@"http://maps.google.com/"]
		   || [sUrl hasPrefix:@"http://www.youtube.com/"]
		   || [sUrl hasPrefix:@"http://phobos.apple.com/WebObjects/"]
		   || [sUrl hasPrefix:@"http://itunes.apple.com/WebObjects/"]
		   || [sUrl rangeOfString:@"&tag=external"].location != NSNotFound
		   || [sUrl rangeOfString:@"?tag=external"].location != NSNotFound)) {
		   return ![[UIApplication sharedApplication] openURL:url];
	   }
	return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)web {
	
	[_loading setHidden:NO];
    [_loading startAnimating];
    [_textTitle setHidden:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)web {
	[webView setHidden:NO];
	
    [_loading setHidden:YES];
    [_loading stopAnimating];
    
    [_textTitle setHidden:NO];
    
	[retryButton setHidden:YES];
	[errorLabel setHidden:YES];
}

- (void)webView:(UIWebView *)web didFailLoadWithError:(NSError *)error {
	if([error code] == NSURLErrorCancelled) // this error (-999) can be safely ignored
		return;
	if([[error domain] isEqual:NSURLErrorDomain]) {
		NSURL *url = [[error userInfo] objectForKey:@"NSErrorFailingURLKey"];
		retryUrl = [url copy];
		
		[webView setHidden:YES];
		[titleLoadingIndicator setHidden:YES];
		[titleLoadingIndicator stopAnimating];
		[retryButton setHidden:NO];
		[errorLabel setHidden:NO];
		
//		if([[NVAppDelegate instance] netStatus] == NotReachable) {
//			errorLabel.text = @"无法连接到服务，请检查网络连接是否可用";
//		} else {
//			errorLabel.text = @"服务暂时不可用，请稍后再试";
//		}
	}
}

- (IBAction)retry:(id)sender {
	[retryButton setHidden:YES];
	[errorLabel setHidden:YES];
	
	if(retryUrl) {
		[webView loadRequest:[NSURLRequest requestWithURL:retryUrl]];
	} else {
		[webView reload];
	}
}

- (void)setTitleStr:(NSString*)title{
    _textTitle.text = title;
}

- (IBAction)actionBack:(id)sender {
    
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.webView.delegate = nil;	
	self.webView = nil;
	self.errorLabel = nil;
	self.retryButton = nil;
	
    [self setTextTitle:nil];
    [self setLoading:nil];
	[super viewDidUnload];
}

- (void)dealloc {
    self.webView.delegate = nil;
	self.webView = nil;
    self.errorLabel = nil;
	self.retryButton = nil;
    [_rawData release];
    [_loading release];
    [_textTitle release];

    [super dealloc];
}

- (IBAction)actionRemove:(id)sender {
    [[MsgManager sharedInstance] removeMsg:_rawData];
    [self actionBack:nil];
    [[iToast makeText:@"消息已删除"] show];
}

@end
